package com.xinyu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinyu.domain.Module;
import com.xinyu.service.IModuleService;
import com.xinyu.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author: XinYu
 * @since 2025-02-17
 */
@RestController
@RequestMapping("/module")
public class ModuleController {

    @Autowired
    private IModuleService moduleService;

    @GetMapping("/queryAll")
    public ResponseEntity<BaseResult> queryAll() {
        // 使用 LambdaQueryWrapper（推荐，类型安全）
        LambdaQueryWrapper<Module> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Module::getIsDelete, 0); // 只查询未删除的数据

        List<Module> list = moduleService.list(wrapper);
        return ResponseEntity.ok(BaseResult.ok(list));
    }
    /**
     * 分页查询 Module
     */
    @PostMapping("/list")
    public ResponseEntity<BaseResult> list(@RequestBody Map<String, Integer> params) {
        int pageNum = params.get("pageNum");
        int pageSize = params.get("pageSize");
        Page<Module> page = new Page<>(pageNum, pageSize);
        IPage<Module> result = moduleService.selectModulePage(page);
        return ResponseEntity.ok(BaseResult.ok(result));
    }

    /**
     * 新增 Module
     */
    @PostMapping("/save")
    public ResponseEntity<BaseResult> save(@RequestBody Module module) {
        moduleService.save(module);
        return ResponseEntity.ok(BaseResult.ok());
    }

    /**
     * 修改 Module
     */
    @PostMapping("/update")
    public ResponseEntity<BaseResult> update(@RequestBody Module module) {
        moduleService.updateById(module);
        return ResponseEntity.ok(BaseResult.ok());
    }

    /**
     * 删除 Module
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<BaseResult> delete(@PathVariable Integer id) {
        Module module = moduleService.getById(id);
        module.setIsDelete(1);
        moduleService.updateById(module);
        return ResponseEntity.ok(BaseResult.ok());
    }
}

